# NOT RUN {
data(base_did)
# Creating a fixest_panel object
pdat = panel(base_did, ~id+period)
# Subselections of fixest_panel objects bookkeeps the leads/lags engine
pdat_small = pdat[!pdat$period %in% c(2, 4), ]
a = feols(y~l(x1, 0:1), pdat_small)
# we obtain the same results, had we created the lags "on the fly"
base_small = base_did[!base_did$period %in% c(2, 4), ]
b = feols(y~l(x1, 0:1), base_small, panel.id = ~id+period)
etable(a, b)
# Using data.table to create new lead/lag variables
if(require("data.table")){
pdat_dt = panel(as.data.table(base_did), ~id+period)
# Variable creation
pdat_dt[, x_l1 := l(x1)]
pdat_dt[, c("x_l1", "x_f1_2") := .(l(x1), f(x1)**2)]
# Estimation on a subset of the data
# (the lead/lags work appropriately)
feols(y~l(x1, 0:1), pdat_dt[!period %in% c(2, 4)])
}
# }
Run the code above in your browser using DataLab